package cn.com.runnable;

import cn.com.task.TaskDemo;
import org.junit.Test;

public class MainTest {

    /**
     * 匿名内部类
     */
    @Test
    public void targetDemo2Test(){

        Runnable runnable1 = new Runnable(){
            public void run() {
                //获取线程名称
                String threadName = Thread.currentThread().getName();
                for (int i = 0; i < 10; i++) {
                    System.out.println(threadName + "：Hello world!");
                    try {
                        Thread.sleep(1000L);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        };

        Runnable runnable2 = new Runnable(){
            public void run() {
                //获取线程名称
                String threadName = Thread.currentThread().getName();
                for (int i = 0; i < 10; i++) {
                    System.out.println(threadName + "：Hello world!");
                    try {
                        Thread.sleep(1000L);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        };

        Thread t1 = new Thread(runnable1, "t1");
        Thread t2 = new Thread(runnable2, "t2");
        t1.start();
        t2.start();
    }


    /**
     * 线程类，实现Runnable接口
     */
    @Test
    public void targetDemo1Test(){
        TaskDemo targetDemo = new TaskDemo(1);
        Thread t1 = new Thread(targetDemo, "t1");
        Thread t2 = new Thread(targetDemo, "t2");
        t1.start();
        t2.start();
    }
}
